package com.lagou.web;

import com.lagou.domin.Admin;
import com.lagou.service.AdminService;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.Map;

public class CheckServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //通过AdminService查询用户名和密码是否正确
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        AdminService as = new AdminService();
        if(as.check(new Admin(username, password))){
            //用户名密码正确则在session中存入用户名和密码并跳转到首页
            HttpSession session = req.getSession();
            session.setAttribute("username", username);
            session.setAttribute("password", password);
            Cookie cookie1 = new Cookie("username", username);
            Cookie cookie2 = new Cookie("password", password);
            resp.addCookie(cookie1);
            resp.addCookie(cookie2);
            resp.sendRedirect("http://localhost:63342/stage2_module3_homework/web/home.html");
        }else{
            resp.sendRedirect("http://localhost:63342/stage2_module3_homework/web/logIn.html");
            resp.getWriter().write("用户名或密码错误");
        }
    }
}
